<?php
require_once("inc.admin.php");

set_time_limit(0);

$this_title="$vars[admin_title] &raquo; ".__("Commission Report");
$page_title=__("Commission Report");

if(!$pv["task"]["Commission Report"]){
	$errmsg="<h2>$page_title</h2>".format_err(__("You do not have the privilege to access Commission Report."));

	print format_admin_page($errmsg, $this_title);
	exit();
}

$country_code=$vars['common']['country_code'];
array_shift($country_code);
$country_code_t=$vars['common']['country_code_text'];
foreach($country_code_t as $code=>$cname){
 $country_code_d[$code]=__($cname);
}

$comm_date=$get_s['comm_date']? $get_s['comm_date'] : ndate('Y').ndate('n');
$year=substr($comm_date, 0, 4);
$month=substr($comm_date, 4, 2);
$calc_date['start'] = date('Y-m-d H:i:s', mktime(0,0,0,$month,1,$year));
$calc_date['end'] = date('Y-m-d H:i:s', mktime(23,59,59,$month+1,0,$year));

//#####PRINT BONUS REPORT#####
if($_GET['print_comm']){
	$print_result = print_bonus_summary($year, $month, $get_s['uid']);
	
	print $print_result['header'].$print_result['bonus_summary'];
	exit;
}

if($_GET['__req']){
	rebuild(1);
	print_r('finish');exit;
}

$year_start=2009;
$month_start=3;
$year_end=ndate('Y');
$month_end=ndate('n');
$ii=0;
if($year_end > $year_start){
	for($j=$month_start;$j<=12;$j++){
		$yearmonth=strval($year_start).strval($j);
		$r_comm_date[$ii++]=$yearmonth;
		$r_comm_date_d[$yearmonth]=date('M-Y', mktime(0,0,0,$j,1,$year_start));
	}
	for($i=$year_start+1;$i<=$year_end;$i++){
		for($j=1;$j<=$month_end;$j++){
			$yearmonth=strval($i).strval($j);
			$r_comm_date[$ii++]=$yearmonth;
			$r_comm_date_d[$yearmonth]=date('M-Y', mktime(0,0,0,$j,1,$i));
		}
	}
}else{
	for($i=$year_start;$i<=$year_end;$i++){
		for($j=$month_start;$j<=$month_end;$j++){
			$yearmonth=strval($i).strval($j);
			$r_comm_date[$ii++]=$yearmonth;
			$r_comm_date_d[$yearmonth]=date('M-Y', mktime(0,0,0,$j,1,$i));
		}
	}
}
$comm_date_select=build_select($r_comm_date, $r_comm_date_d, $get_s['comm_date']? $get_s['comm_date'] : ndate('Y').ndate('n'), 'comm_date');

//javascript
ob_start();
?>
<script type="text/javascript" src="<?php echo JS_URL."/get_file_gzip.php?file=".urlencode("jquery.js,fn/fn_urlencode.js"); ?>"></script>
<script type="text/javascript">
	jQuery(document).ready(function(j){
		j('input[@name=print_btn]').click(function(){
			window.open('<?php echo addslashes("$this_file?print_comm=1&comm_date="); ?>' + j('select[@name=comm_date] > option:selected').val() + '&uid=' + urlencode(j('input[@name=uid]').val()), 'print_window', 'status=1, toolbar=1, location=1, menubar=1, width=700, height=700');
		});
		j('input[@name=uid]').keypress(function(e){
			if (e.which == 13) {
				j('input[@name=print_btn]').click();
				return false;
			}
		});
		j('input[@name=generate_btn]').click(function(){
			j('input[@name=print_comm]').val('');
			j('form[@name=comm_form]').submit();
		});
		j('form[@name=commsum_form]').submit(function(){
			if(!confirm('<?php echo addslashes(__("Update the bonus remark, are you sure?")); ?>')){
				return false;
			}
		});
	});
</script>
<?php
$jvscript = ob_get_clean();

$comm_form=
"<form name='comm_form' method='get' action='$this_file'>
<input type='hidden' name='__req' value='1' />
<input type='hidden' name='print_comm' />
Commission Date: $comm_date_select <input type='button' name='generate_btn' value=\"".__("Generate")."\" /><br />
Print only report for User ID, separate by comma (,), leave blank to print all members: <input type='text' name='uid' /> <input type='button' name='print_btn' value=\"".__("Print Bonus Summary")."\" />
</form>".$msg.$errmsg;

$content="<h2>$page_title</h2>".$comm_form.$comm_table;

print format_admin_page($content, $this_title, $jvscript);

	global $arrTree;
	function rebuild($rootid=1)
	{
		$t = @mysql_num_rows($r=mysql_query("select id,matrix_upline from gg_users where matrix_upline>0 or id=1 order by id"));
		if($t<=0)
		{
			return false;
		}
		$arrTree = array();
		while($user = mysql_fetch_assoc($r))
		{
			$nParentID = $user['matrix_upline'];
			$dataTemp  = array();
			$dataTemp['id']    = $user['id'];
			$arrTree[$nParentID][] = $dataTemp;
		}

		$rootID = 0;
		if((isset($arrTree[0])) && (sizeof($arrTree[0])>0) )
		{
			$rootID = $arrTree[0][0]['id'];
		}

		if($rootID<=0)
		{
			return false;
		}

		rebuild_tree($rootID,1,1,1, $arrTree);
		return true;

	}

	function rebuild_tree($nParentID,$left,$nlevel,$seq, $arrTree)
	{
		$right = $left+1;

		$subNodeInfo   = @(isset($arrTree[$nParentID]))?$arrTree[$nParentID]:array();

		$subSize       = sizeof($subNodeInfo);
		if($subSize>0)
		$isparent = 1;
		else
		$isparent=0;

		if($subSize>0)
		{
			$seqno = 0;
			foreach($subNodeInfo as $key=>$nodeInfo)
			{
				$right = rebuild_tree($nodeInfo['id'],$right,$nlevel+1,$seqno+1, $arrTree);
				$seqno++;
			}
		}

		mysql_query("update gg_users set matrix_left = $left, matrix_right = $right where id = $nParentID");
		
		return $right+1;
	}
?>